package com.jar.exercise.多线程.实现线程的方式;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 实现Callable接口来实现多线程操作
 * 1、实现Callable接口,重写call方法
 * 2、
 */
public class AnonyCallable {
	public static void main(String[] args) {

		Callable<String> cl = () -> {

			System.out.println(Thread.currentThread().getName() + "正在行军~~~");

			System.out.println(Thread.currentThread().getName() + "遭遇敌军~~~");

			System.out.println(Thread.currentThread().getName() + "奋勇杀敌！！！！");

			return "战斗胜利，俘虏敌军50000人";

		};

		try {
			FutureTask<String> ft = new FutureTask<>(cl);
			new Thread(ft, "李存孝部队").start();

			Thread.currentThread().setName("李存勖部队");
			System.out.println(Thread.currentThread().getName() + "休整3000ms");
			Thread.sleep(3000);

			System.out.println(Thread.currentThread().getName() + "整顿完毕，等待友军消息...");
			String str = ft.get();
			System.out.println("李存勖部队得知友军消息为：" + str);
		} catch (InterruptedException | ExecutionException e) {
			e.printStackTrace();
		}


	}
}
